package com.thiswhyme.common.utils.codec;

import com.thiswhyme.common.expection.BusinessException;
import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;

/**
 * @description: DES工具
 * @date: 2021/9/13
 */
@Slf4j
public class DesUtils {

    /**
     * 可配置到Constant中，并读取配置文件注入,8的倍数,自己定义
     */
    private static final String KEY = "ujnyhb8765432*&%$#@~1234biwyzp.";

    /**
     * 加密
     * @param content 原文
     * @param key 密钥
     * @return 返回加密结果
     */
    public static String encrypt(String content, String key) {
        String result = null;
        try {
            SecureRandom random = new SecureRandom();
            DESKeySpec desKey = new DESKeySpec(key.getBytes());
            //创建一个密匙工厂，然后用它把DESKeySpec转换成
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(EncryptAlgorithm.DES.name());
            SecretKey securekey = keyFactory.generateSecret(desKey);
            //Cipher对象实际完成加密操作
            Cipher cipher = Cipher.getInstance(EncryptAlgorithm.DES.name());
            //用密匙初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
            //现在，获取数据并加密
            //正式执行加密操作
            byte[] bytes = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
            result = CodecUtils.bytes2Base64String(bytes);
        } catch (Exception ex) {
            log.error("DES加密异常", ex);
            throw new BusinessException("DES加密异常");
        }
        return result;
    }

    /**
     * 解密
     * @param encryptStr 解密的字符串
     * @param key 解密的key值
     * @return byte[]
     * @throws Exception
     */
    public static String decrypt(String encryptStr, String key) {
        String result = null;
        try {
            // DES算法要求有一个可信任的随机数源
            SecureRandom random = new SecureRandom();
            // 创建一个DESKeySpec对象
            DESKeySpec desKey = new DESKeySpec(key.getBytes());
            // 创建一个密匙工厂
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(EncryptAlgorithm.DES.name());
            // 将DESKeySpec对象转换成SecretKey对象
            SecretKey securekey = keyFactory.generateSecret(desKey);
            // Cipher对象实际完成解密操作
            Cipher cipher = Cipher.getInstance(EncryptAlgorithm.DES.name());
            // 用密匙初始化Cipher对象
            cipher.init(Cipher.DECRYPT_MODE, securekey, random);
            // 真正开始解密操作
            byte[] bytes = cipher.doFinal(CodecUtils.base64String2Bytes(encryptStr));
            result = new String(bytes);
        } catch (Exception ex) {
            log.error("DES解密异常", ex);
            throw new BusinessException("DES解密异常");
        }
        return result;
    }

    /**
     * DES加密
     * @param content 原文内容
     * @return 返回String类型，加密字符
     * @throws Exception 异常
     */
    public static String encrypt(String content) {
        return encrypt(content, KEY);
    }

    /**
     * DES解密
     * @param encryptStr 密文
     * @return 返回原文内容
     * @throws Exception 异常
     */
    public static String decrypt(String encryptStr) {
        return decrypt(encryptStr, KEY);
    }

}
